package com.photologics.logic;

import android.graphics.Bitmap;

public class ImageProcessing {

	// BitmapFactory.decodefile(���)

	public static void getImage(Bitmap oriBitmap) {
		toLogic(oriBitmap);
	}

	private static Bitmap scaleImage(Bitmap original, int newWidth,
			int newHeight) {
		int oriHeight = original.getHeight();
		int oriWidth = original.getWidth();

		
		int[] rawInput = new int[oriHeight * oriWidth];
		for (int i = 0; i < oriHeight; i++)
			for (int j = 0; j < oriWidth; j++)
				rawInput[i * oriWidth + j] = original.getPixel(j, i);

		// public void getPixels(int[] pixels, int offset, int stride, int x,
		// int y, int width, int height)
		int[] rawOutput = new int[newWidth * newHeight];

		// YD compensates for the x loop by subtracting the width back out
		int YD = (original.getHeight() / newHeight) * original.getWidth()
				- original.getWidth();
		int YR = original.getHeight() % newHeight;
		int XD = original.getWidth() / newWidth;
		int XR = original.getWidth() % newWidth;
		int outOffset = 0;
		int inOffset = 0;

		for (int y = newHeight, YE = 0; y > 0; y--) {
			for (int x = newWidth, XE = 0; x > 0; x--) {
				rawOutput[outOffset++] = rawInput[inOffset];
				inOffset += XD;
				XE += XR;
				if (XE >= newWidth) {
					XE -= newWidth;
					inOffset++;
				}
			}
			inOffset += YD;
			YE += YR;
			if (YE >= newHeight) {
				YE -= newHeight;
				inOffset += original.getWidth();
			}
		}

		Bitmap reImage = Bitmap.createBitmap(newWidth, newHeight,
				Bitmap.Config.ARGB_8888);
		
		reImage.setPixels(rawOutput,0,newWidth,0,0,newWidth,newHeight);

		return reImage;
	}

	private static void toLogic(Bitmap oriImage) {

		int width = 303;
		int height = 303;

		oriImage = scaleImage(oriImage, width, height);

		LogicHelper.toLogic = null;
		LogicHelper.toLogic = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);

		int[][] edgeMask_x = { { -1, 0, 1 }, { -1, 0, 1 }, { -1, 0, 1 } };
		int[][] edgeMask_y = { { -1, -1, -1 }, { 0, 0, 0 }, { 1, 1, 1 } };

		int[][] maskResult_x = new int[height][width];
		int[][] maskResult_y = new int[height][width];

		int heightm1 = height - 1;
		int widthm1 = width - 1;
		int newValue_x = 0, newValue_y = 0;

		float constVal1, constVal2;

		// 마스킹
		for (int i = 1; i < heightm1; i++) {
			for (int j = 1; j < widthm1; j++) {

				newValue_x = 0;
				newValue_y = 0;

				for (int mr = 0; mr < 3; mr++)
					for (int mc = 0; mc < 3; mc++) {
						newValue_x += edgeMask_x[mr][mc]
								* (oriImage.getPixel(j + mc - 1, i + mr - 1) & 0x00FF0000 >> 16);
						newValue_y += edgeMask_y[mr][mc]
								* (oriImage.getPixel(j + mc - 1, i + mr - 1) & 0x00FF0000 >> 16);
					}
				maskResult_x[i][j] = newValue_x;
				maskResult_y[i][j] = newValue_y;

			}
		}

		//maskResult
		for (int i = 1; i < height; i++)
			for (int j = 1; j < width; j++) {
				constVal1 = maskResult_x[i][j];
				constVal2 = maskResult_y[i][j];
				if (constVal1 < 0)
					constVal1 = -constVal1;
				if (constVal2 < 0)
					constVal2 = -constVal2;
				maskResult_x[i][j] = (int) (constVal1 + constVal2);
 
			}

		// min, max 

		int min = Integer.MAX_VALUE;
		int max = Integer.MIN_VALUE;
		for (int i = 1; i < height; i++) {
			for (int j = 1; j < width; j++) {

				if (maskResult_x[i][j] < min)
					min = maskResult_x[i][j];
				if (maskResult_x[i][j] > max)
					max = maskResult_x[i][j];
			}
		}

		//흑백
		constVal1 = (float) (255.0 / (max - min));
		constVal2 = (float) (-255.0 * min / (max - min));

		for (int i = 1; i < height; i++) {
			for (int j = 1; j < width; j++) {

				int value = maskResult_x[j][i];
				value = (int) (constVal1 * value + constVal2);

				int eachNewRGB = 0xff000000 | (value << 16) | (value << 8)
						| value;
				LogicHelper.toLogic.setPixel(i, j, eachNewRGB);

			}
		}
		
		

		//색상반전
		for (int i = 1; i < height; i++) {
			for (int j = 1; j < width; j++) {
				int newRGB = 0xffffffff - (LogicHelper.toLogic.getPixel(i, j) & 0x00ffffff);

				LogicHelper.toLogic.setPixel(i, j, newRGB);

			}
		}
		
		//공백제거
		LogicHelper.toLogic = Bitmap.createBitmap(LogicHelper.toLogic, 3, 3, 300 , 300);
		height = LogicHelper.toLogic.getHeight();
		width = LogicHelper.toLogic.getWidth();


		//평균
		int sum = 0;

		for (int i = 1; i < height; i++) {
			for (int j = 1; j < width; j++) {
				sum = sum + (LogicHelper.toLogic.getPixel(i, j) & 0x000000ff);
			}
		}
		int avr = sum / (height * width);

		//이진화
		for (int i = 1; i < height; i++) {
			for (int j = 1; j < width; j++) {
				if ((LogicHelper.toLogic.getPixel(i, j) & 0x000000ff) > avr)
					LogicHelper.toLogic.setPixel(i, j, 0xffffffff);
				else
					LogicHelper.toLogic.setPixel(i, j, 0xff000000);
			}
		}

	}

	public static Bitmap sumPixel(Bitmap sourceImage, int divideNumber) {

		int dividedPixel = 300 / divideNumber; // ������ �ȼ���
		int dividedPixelBorder = dividedPixel * dividedPixel / 2;

		Bitmap preViewImage;
		preViewImage = Bitmap.createBitmap(divideNumber, divideNumber,
				Bitmap.Config.ARGB_8888);

		for (int p = 0; p < divideNumber; p++)
			for (int q = 0; q < divideNumber; q++) {
				int sumColor = 0;
				for (int i = q * dividedPixel; i < (q + 1) * dividedPixel; i++)
					for (int j = p * dividedPixel; j < (p + 1) * dividedPixel; j++)
						sumColor = sumColor
								+ (sourceImage.getPixel(i, j) & 0x000000ff);
				int cntBlack = sumColor / 255;
				if (cntBlack > dividedPixelBorder)
					preViewImage.setPixel(q, p, 0xff000000);
				else
					preViewImage.setPixel(q, p, 0xffffffff);

			}
		
		//경계선처리
		int h = preViewImage.getHeight();
		int count = divideNumber/25;
			for(int j = 1; j<count; j++)
				for(int i = 0; i<h; i++) {
					preViewImage.setPixel(i, h*j/count, 0xffffcc66);
					preViewImage.setPixel(h*j/count, i, 0xffffcc66);
				}
		return preViewImage;
	}

	public static byte[][] makeArray(Bitmap sourceImage, int divideNumber) {

		int dividedPixel = 300 / divideNumber; 
		int dividedPixelBorder = dividedPixel * dividedPixel / 2;

		byte[][] logicArray = new byte[divideNumber][divideNumber];

		for (int p = 0; p < divideNumber; p++)
			for (int q = 0; q < divideNumber; q++) {
				int sumColor = 0;
				for (int i = q * dividedPixel; i < (q + 1) * dividedPixel; i++)
					for (int j = p * dividedPixel; j < (p + 1) * dividedPixel; j++)
						sumColor = sumColor
								+ (sourceImage.getPixel(i, j) & 0x000000ff);
				int avrColor = sumColor / 255;
				if (avrColor > dividedPixelBorder)
					logicArray[q][p] = 1;
				else
					logicArray[q][p] = 0;
			}
		return logicArray;
	}

}
